package com.lzxuni.modules.disease.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.disease.entity.DiseaseCritical;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.disease.service.DiseaseCriticalService;
import org.springframework.web.bind.annotation.RestController;
import com.lzxuni.modules.common.controller.BaseController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * <p>
 * 病情管理--病情状态临界值 前端控制器
 * </p>
 *
 * @author gyl
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/Disease/critical")
public class DiseaseCriticalController extends BaseController {

    @Autowired
    private DiseaseCriticalService diseaseCriticalService;
    private String templatesPath="/Disease/critical";

    @Autowired
    private FileEntityService fileEntityService;
    private static String typeName = "病情界限图片";
    private static String ywType = "dis_img";
    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/Disease/critical/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        DiseaseCritical diseaseCritical = JSON.parseObject(queryJson, DiseaseCritical.class);
        PageData pageData = getPageData(diseaseCriticalService.queryPage(pageParameter, diseaseCritical));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("/Disease/critical/form");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(keyValue);
        fileBeanCustom.setYwType(ywType);
        mv.addObject("imgCount", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        mv.addObject("id", keyValue);
        return mv;
    }

    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        diseaseCriticalService.removeById(keyValue);
        return R.ok("删除成功");
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue,DiseaseCritical diseaseCritical, String dis_imgs) throws Exception{
        diseaseCritical.setCriticalId(keyValue);
        diseaseCritical.setCropId(diseaseCritical.getCropModel());
        if(StringUtils.isEmpty(diseaseCritical.getCriticalId())){
            String ywId = UuidUtil.get32UUID();
            if (!com.lzxuni.common.utils.StringUtils.isEmpty(dis_imgs) && !"&amp;nbsp;".equals(dis_imgs)) {
                fileEntityService.insert(dis_imgs.replace("&quot;", "\""), ywId, typeName, ywType, null);
            }
            diseaseCritical.setCriticalId(ywId);
            diseaseCritical.setCreateTime(new Date());
             diseaseCriticalService.save(diseaseCritical);
             return R.ok("保存成功");
        }else{
            fileEntityService.deleteByYwId(keyValue);
            if (!StringUtils.isEmpty(dis_imgs) && !"&amp;nbsp;".equals(dis_imgs)) {
                fileEntityService.insert(dis_imgs.replace("&quot;", "\""), keyValue, typeName, ywType, null);
            }

            diseaseCriticalService.updateById(diseaseCritical);
            return R.ok("修改成功");
        }
    }


}

